import qlib
from qlib.data.filter import NameDFilter

from multiprocessing import freeze_support
qlib.init(provider_uri='~/.qlib/qlib_data/fund_data')

from qlib.data import D
instruments = D.instruments(market='all')
all = D.list_instruments(instruments=instruments, start_time='2010-01-01', as_list=True)
print(all)



if __name__ == '__main__':
    freeze_support()
    instruments = D.instruments(market='all')
    all = D.list_instruments(instruments=instruments, as_list=True)
    fields = ['$close', '$close / Ref($close, 20) - 1']
    df = D.features(all, fields, start_time='2010-01-01',
                    freq='day')

    #print(df)


    df.reset_index(level=0, inplace=True)
    df.sort_index(ascending=True, inplace=True)
    print(df)
    df_bar = df.loc['2021-12-09']
    print(df_bar)
    df_bar.sort_values(by='$close / Ref($close, 20) - 1',ascending=False,inplace=True)
    print(df_bar)
    top = df_bar.iloc[:200]

    from common.config import get_config_path
    top.to_csv(get_config_path()+'/etf_top.csv')
